package com.y2w.gulimall.auth.controller;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.y2w.common.utils.R;
import com.y2w.gulimall.auth.fegin.MemberFeginService;
import com.y2w.common.vo.MemberRespVo;
import com.y2w.gulimall.auth.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 处理社交登录请求
 */
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeginService memberFeginService;

    @GetMapping("/oauth2.0/github/success")
    public String github(@RequestParam("code") String code){

        //根据code换取accessToken,post
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("code",code);
        paramMap.put("client_id","c875959692863ac8895a");
        paramMap.put("client_secret","5e9392778ff6348878e3e4b1ba67f2a6a0f7f36c");
        String post = HttpUtil.post("github.com/login/oauth/access_token", paramMap);
        System.out.println(post);
        //登陆成功跳回首页
        return "redirect:http://gulimall.com";
    }

    @GetMapping("/oauth2.0/gitee/success")
    public String gitee(@RequestParam("code") String code, HttpSession session){

        //https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}
        //根据code换取accessToken,post
        Map<String,Object> paramMap=new HashMap<>();
        paramMap.put("code",code);
        paramMap.put("grant_type","authorization_code");
        paramMap.put("redirect_uri","http://auth.gulimall.com/oauth2.0/gitee/success");
        paramMap.put("client_id","75cd2b98ca0f4501ab66ca2b1b0ae03e33de27e0285ff2e8ad06ef2f4531e426");
        paramMap.put("client_secret","9bb9496e4f7195bc9e57da30a6f9185417dbff727b513fb54810b2ee5d52cfa8");
        String response = HttpUtil.post("https://gitee.com/oauth/token", paramMap);
        SocialUser socialUser = JSON.parseObject(response, SocialUser.class);
        if(socialUser!=null){
            String accessToken = socialUser.getAccess_token();
            Map<String,Object> getMap=new HashMap<>();
            getMap.put("access_token",accessToken);
            String s= HttpUtil.get("https://gitee.com/api/v5/user", getMap);
            HashMap hashMap = JSON.parseObject(s, HashMap.class);
            Map map=new HashMap();
            map.put("id",hashMap.get("id"));
            map.put("name",hashMap.get("name"));

            R r = memberFeginService.oauth2Login(socialUser,map.toString());
            if(r.getCode()==0){
                MemberRespVo respVo = r.getData("data", new TypeReference<MemberRespVo>() {
                });
                session.setAttribute("loginUser",respVo);
                //登陆成功跳回首页
                return "redirect:http://gulimall.com";
            }else{
                return "redirect:http://auth.gulimall.com/login.html";
            }
            //return "redirect:http://gulimall.com";
        }else{
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}
